<?php
/**
 * (ajax) 提货sku
 */
namespace App\Admin\Ajax\Shipping;

use App\Models\Shipping\SSku;
use App\Models\Shipping\SSkuApply;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
class SkuAjax {

    /**
     * 申请页面
     * @param $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function apply($id){
        //提货产品
        $shippingSku = SSku::query()->find($id);

        $shippingDate = SSkuApply::initShippingDate(); //提货日期
        $applyNum = SSkuApply::getApplyNum($shippingSku->area_id,$shippingSku->psku_id,array_key_first($shippingDate),array_key_last($shippingDate)); //提货数量
        $re = [];
        foreach($shippingDate as $date => $label){
            $num = array_key_exists($date,$applyNum) ? $applyNum[$date] : 0;
            $re[$date]['label'] = $label;
            $re[$date]['has_apply_num'] = $num;
            $re[$date]['this_apply_num'] = $num;
        }
        return view('shipping.sku.apply',compact('id','re'));
    }

    /**
     * 提货申请
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function doApply($param){
        try{
            $id = $param['id'];
            $data = $param['detail'];
            DB::beginTransaction();
            $shippingSku = SSku::query()->find($id);
            if(!in_array($shippingSku->status,[1,3,4])){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败：状态有误！']);
            }
            if($shippingSku->is_lock == 1){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败：锁定状态！']);
            }
            $shippingSku->status = 2; //待审批
            $shippingSku->seller_id = Admin::user()->id;
            $flag = $shippingSku->save();
            $i = 0;
            foreach($data as $date => $item){
                $apply = SSkuApply::query()->firstOrNew([
                    'area_id' => $shippingSku->area_id,
                    'psku_id' => $shippingSku->psku_id,
                    'shipping_date' => $date,
                ]);
                $apply->last_apply_num = $apply->apply_num ?: 0;
                $apply->apply_num = intval($item['apply_num']);
                $flag1 = $apply->save();
                $flag1 && $i++;
            }
            if($flag && $i == count($data)){
                DB::commit();
                return response()->json(['status' => true, 'message' => '操作成功！']);
            }else{
                DB::rollBack();
                return response()->json(['status' => true, 'message' => '操作失败！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }

    /**
     * 撤销申请
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function revoke($id){
        try{
            DB::beginTransaction();
            $shippingDate = SSkuApply::initShippingDate();
            $sDate = array_key_first($shippingDate);
            $eDate = array_key_last($shippingDate);
            $shippingSku = SSku::query()->find($id);
            if($shippingSku->status != 2){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败：状态有误！']);
            }
            $shippingSku->status = 1; //未申请
            $flag = $shippingSku->save();
            if(!$flag){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }
            $apply = SSkuApply::query()
                ->where('area_id',$shippingSku->area_id)
                ->where('psku_id',$shippingSku->psku_id)
                ->where('shipping_date','>=',$sDate)
                ->where('shipping_date','<=',$eDate)
                ->get();
            $flag1 = false;
            foreach($apply as $_apply){
                $_apply->apply_num = $_apply->last_apply_num;
                $flag2 = $_apply->save();
                if(!$flag2){
                    $flag1 = false;
                    break;
                }
                $flag1 = true;
            }
            if($flag1){
                DB::commit();
                return response()->json(['status' => true, 'message' => "撤回成功！"]);
            }else{
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统异常！']);
        }
    }
}
